--File ： ChapterView.lua
--Author : zee
--Date   : 2015/4/8
--Description : 关卡主界面 chapter.fla 第一帧



require "BattleLayer/BStone"
require "BattleLayer/BMap"
require "BattleLayer/BHero"
require "BattleLayer/GameUI"
require "BattleLayer/GameHeart"
require "BattleLayer/CrashCenter"
require "BattleLayer/TouchCenter"



BattleLayer = class("BattleLayer",function()
    -- 继承
    local ret = cc.Layer:create()

    ret.hero = nil
    ret.gameUI = nil
    ret.map = nil
    ret.ary_stone = {}
    ret.ary_ary_monst = {}

    return ret
end)

BattleLayer.__index = BattleLayer
BattleLayer.__instance = nil


function BattleLayer:create()
    local ret = BattleLayer:new()
    BattleLayer.__instance = ret
    return ret
end

function BattleLayer:getInstance()
    if( BattleLayer.__instance==nil ) then 
        BattleLayer.__instance = BattleLayer:create()
    end
    return BattleLayer.__instance
end



--------------------------------------------------------
-- 初始化
--------------------------------------------------------
function BattleLayer:start()
    -- 先要创建碰撞体系。
    CrashCenter:create()

    -- 创建地图
    self.map = BMap:create()
    self:addChild( self.map )

    -- 游戏判断逻辑
    -- GameHeart:getInstance():start()

    -- 创建英雄
    self.hero = BHero:create()
    self:addChild(self.hero)

    -- 添加逐帧方法:
    local function frameEvent(dt) 
        self:onFrame(dt) 
    end
    self:scheduleUpdateWithPriorityLua( frameEvent, 0 )

    -- UI
    self.gameUI = GameUI:create()

    -- 手势
    self:addTouch()

    -- 怪物
    -- test 

    for i=1,100,1 do
        local st = BStone:create()
        self.map:addChild( st )
        st:setPosition( math.random(1,500), i*50 )
    end 
end




function BattleLayer:onFrame(dt)
    self.map:onFrame(dt)
    self.hero:onFrame(dt)
end







-- 手势
function BattleLayer:addTouch()
     -- 手势
    local function onTouchBegan( touch, event ) return true end
    local function onTouchMoved( touch, event ) return true end
    local function onTouchEnded( touch, event ) return true end
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches( false )
    listener:registerScriptHandler( onTouchBegan , cc.Handler.EVENT_TOUCH_BEGAN )  
    listener:registerScriptHandler( onTouchMoved , cc.Handler.EVENT_TOUCH_MOVED )
    listener:registerScriptHandler( onTouchEnded , cc.Handler.EVENT_TOUCH_ENDED ) 
    -- self:getEventDispatcher():addEventListenerWithSceneGraphPriority( listener, self )

    -- 多点触摸
    local function onTouchesBegan( touches , event )
        TouchCenter:getInstance():onTouchBegan(touches, event)
    end
    local function onTouchesMoved( touches, event )  
        TouchCenter:getInstance():onTouchMoved(touches, event)
    end
    local function onTouchesEnded( touches, event )  
        TouchCenter:getInstance():onTouchEnd(touches, event)
    end 
    local mutilTouch = cc.EventListenerTouchAllAtOnce:create()
    mutilTouch:registerScriptHandler( onTouchesBegan , cc.Handler.EVENT_TOUCHES_BEGAN )  
    mutilTouch:registerScriptHandler( onTouchesMoved , cc.Handler.EVENT_TOUCHES_MOVED )
    mutilTouch:registerScriptHandler( onTouchesEnded , cc.Handler.EVENT_TOUCHES_ENDED ) 
    self:getEventDispatcher():addEventListenerWithSceneGraphPriority( mutilTouch, self )
end


----------------------------------------------------------------
--          被heart调用
----------------------------------------------------------------
function BattleLayer:putOneObj(sType, info)
end



function BattleLayer:makeOneMapShow(x,y,isShow)
end


function BattleLayer:moveHerosToMap( ary, map, func )
end





